package com.globefrog.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import java.io.IOException;

/**
 * @Author: caijinbang
 * @Description:
 * @Date: Create in 2021-03-06
 */
public class JsonUtil {

  public JsonUtil() {
  }

  public static String toJSON(Object obj) {
    return toJSON(com.globefrog.common.util.ObjectMapperBuilder.defaultObjectMapper(), obj);
  }

  public static String toJSON(com.globefrog.common.util.ObjectMapperBuilder builder, Object obj) {
    try {
      return builder.get().writeValueAsString(obj);
    } catch (JsonProcessingException var3) {
      throw new RuntimeException(var3);
    }
  }

  public static <T> T fromJSON(String json, Class<T> exceptClass) {
    return fromJSON(com.globefrog.common.util.ObjectMapperBuilder.defaultObjectMapper(), json, exceptClass);
  }

  public static <T> T fromJSON(com.globefrog.common.util.ObjectMapperBuilder builder, String json, Class<T> exceptClass) {
    try {
      return builder.get().readValue(json, exceptClass);
    } catch (IOException var4) {
      throw new RuntimeException(var4);
    }
  }

  public static <T> T fromJSONWithGeneric(String json, TypeReference<T> genericType) {
    return fromJSONWithGeneric(com.globefrog.common.util.ObjectMapperBuilder.defaultObjectMapper(), json, genericType);
  }

  public static <T> T fromJSONWithGeneric(
      com.globefrog.common.util.ObjectMapperBuilder builder, String json,
      TypeReference<T> genericType) {
    try {
      return builder.get().readValue(json, genericType);
    } catch (IOException var4) {
      throw new RuntimeException(var4);
    }
  }

  public static <T> T fromJSONWithGeneric(String json, JavaType javaType) {
    return fromJSONWithGeneric(com.globefrog.common.util.ObjectMapperBuilder.defaultObjectMapper(), json, javaType);
  }

  public static <T> T fromJSONWithGeneric(
      com.globefrog.common.util.ObjectMapperBuilder builder, String json,
      JavaType javaType) {
    try {
      return builder.get().readValue(json, javaType);
    } catch (IOException var4) {
      throw new RuntimeException(var4);
    }
  }
}
